{{- if .Values.spark.enabled -}}
apiVersion: {{ include "common.capabilities.cronjob.apiVersion" $ }}
kind: CronJob
metadata:
  name: {{ include "jaeger.fullname" . }}-spark
  labels:
    {{- include "jaeger.labels" . | nindent 4 }}
    app.kubernetes.io/component: spark
{{- if .Values.spark.annotations }}
  annotations:
    {{- toYaml .Values.spark.annotations | nindent 4 }}
{{- end }}
spec:
  schedule: {{ .Values.spark.schedule | quote }}
  successfulJobsHistoryLimit: {{ .Values.spark.successfulJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ .Values.spark.failedJobsHistoryLimit }}
  concurrencyPolicy: {{ .Values.spark.concurrencyPolicy }}
  jobTemplate:
    spec:
      {{- if .Values.spark.activeDeadlineSeconds }}
      activeDeadlineSeconds: {{ .Values.spark.activeDeadlineSeconds }}
      {{- end}}
      {{- if .Values.spark.ttlSecondsAfterFinished }}
      ttlSecondsAfterFinished: {{ .Values.spark.ttlSecondsAfterFinished }}
      {{- end }}
      template:
        metadata:
          {{- if .Values.spark.podAnnotations }}
          annotations:
            {{- toYaml .Values.spark.podAnnotations | nindent 12 }}
          {{- end }}
          labels:
            {{- include "jaeger.selectorLabels" . | nindent 12 }}
            app.kubernetes.io/component: spark
            {{- if .Values.spark.podLabels }}
            {{- toYaml .Values.spark.podLabels | nindent 12 }}
            {{- end }}
        spec:
          serviceAccountName: {{ template "jaeger.spark.serviceAccountName" . }}
          {{- with .Values.spark.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.spark.initContainer }}
          initContainers:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          containers:
          - name: {{ include "jaeger.fullname" . }}-spark
            image: {{ .Values.spark.image }}:{{ .Values.spark.tag }}
            imagePullPolicy: {{ .Values.spark.pullPolicy }}
            args:
              {{ include "extra.cmdArgs" ( dict "cmdlineParams" .Values.spark.cmdlineParams ) | nindent 14  }}
            env:
              - name: STORAGE
                value: {{ .Values.storage.type }}
              {{- include "storage.env" . | nindent 14 }}
            {{- if .Values.spark.extraEnv }}
              {{- toYaml .Values.spark.extraEnv | nindent 14 }}
            {{- end }}
            {{- if eq .Values.storage.type "cassandra" }}
              - name: CASSANDRA_CONTACT_POINTS
                value: {{ include "cassandra.contact_points" . }}
            {{- end }}
            {{- if eq .Values.storage.type "elasticsearch" }}
              - name: ES_NODES
                value: {{ include "elasticsearch.client.url" . }}
              - name: ES_NODES_WAN_ONLY
                value: {{ .Values.storage.elasticsearch.nodesWanOnly | quote }}
            {{- end }}
            resources:
              {{- toYaml .Values.spark.resources | nindent 14 }}
            volumeMounts:
            {{- range .Values.spark.extraConfigmapMounts }}
              - name: {{ .name }}
                mountPath: {{ .mountPath }}
                subPath: {{ .subPath }}
                readOnly: {{ .readOnly }}
            {{- end }}
            {{- range .Values.spark.extraSecretMounts }}
              - name: {{ .name }}
                mountPath: {{ .mountPath }}
                subPath: {{ .subPath }}
                readOnly: {{ .readOnly }}
            {{- end }}
            {{- range .Values.spark.extraEmptyDirVolumes }}
              - name: {{ .name }}
                mountPath: {{ .mountPath }}
            {{- end }}
          restartPolicy: OnFailure
          volumes:
          {{- range .Values.spark.extraConfigmapMounts }}
            - name: {{ .name }}
              configMap:
                name: {{ .configMap }}
          {{- end }}
          {{- range .Values.spark.extraSecretMounts }}
            - name: {{ .name }}
              secret:
                secretName: {{ .secretName }}
          {{- end }}
          {{- range .Values.spark.extraEmptyDirVolumes }}
            - name: {{ .name }}
              {{- if eq .medium "Memory" }}
              emptyDir:
                medium: Memory
              {{- else }}
                emptyDir: {}
              {{- end }}
          {{- end }}
        {{- with .Values.spark.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- with .Values.spark.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
        {{- end }}
        {{- with .Values.spark.tolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
        {{- end }}
{{- end -}}
